package com.zjkj.mp.controller.customerManage;



import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zjkj.mp.service.customer.CustomerManagerService;
import com.zjkj.mp.service.customer.GroupService;
import com.zjkj.mp.service.user.UserService;
import com.zjkj.mp.utils.Toolkit;
import com.zjkj.mp.utils.security.SpringSecurityUtils;
import com.zjkj.mp.vo.customer.ContactPerson;
import com.zjkj.mp.vo.customer.Group;
import com.zjkj.mp.vo.user.MpUser;

/**
 * 
 * 新建联系人控制器
 *
 * @author 陈晨
 * @history
 * <TABLE id="HistoryTable" border="1">
 * 	<TR><TD>时间</TD><TD>描述</TD><TD>作者</TD></TR>
 *	<TR><TD>2013-6-9</TD><TD>创建初始版本</TD><TD>陈晨</TD></TR>
 * </TABLE>
 */
@Controller
public class ConcatePersonController {

	@Resource(name = "userServiceImpl")
	private UserService userService;
	@Resource(name = "groupServiceImpl")
	private GroupService groupService;
	@Autowired
	private CustomerManagerService customerManagerService;
	@Resource
	private Toolkit tool;
	
	private static Log log = LogFactory.getLog(ConcatePersonController.class);

	/******************************* 新建联系人 ***********************************/
	
	/**
	 * 跳转至新建联系人页面
	 * @return
	 */
	@RequestMapping("/newCustomer.do")
	public String newCustomer(HttpServletRequest request,HttpServletResponse response) {
		//设置groupId为0，则是查询所有
		List<Group> list = groupService.getGroupList(0);
		request.setAttribute("list", list);
		return "customer/newCustomer";
	}
	
	//执行提交的控制器方法
	@RequestMapping("/doNewcustomer.do")
	public String doNewcustomer(HttpServletRequest request,HttpServletResponse response) {
		String message = "";
		String error = "";
		int userid = ((MpUser)SpringSecurityUtils.getCurrentUser()).getUserId();
		try {		
			ContactPerson person = new ContactPerson();
			String value = null;
			value  = request.getParameter("name");
			person.setName(value);
			value = request.getParameter("movePhoneNumber");
			person.setMovePhoneNumber(value);
			value = request.getParameter("phoneNumber");
			person.setPhoneNumber(value);
			value = request.getParameter("eMail");
			person.seteMail(value);
			value = request.getParameter("birthday");
			value = tool.getTrimDate(value);
			person.setBirthday(value);
			value = request.getParameter("company");
			person.setCompany(value);
			value = request.getParameter("address");
			person.setAddress(value);
			value = request.getParameter("sex");
			person.setSex(value);
			value = request.getParameter("remark");
			person.setRemark(value);
			value = request.getParameter("group");
			person.setGroupId(value);
			person.setCreated(userid);
			customerManagerService.createConactPerson(person);
			message = "创建联系人成功！";
		} catch (Exception e) {
			error = "创建联系人失败!";
			log.error("创建联系人异常",e);
		}
		request.setAttribute("message", message);
		request.setAttribute("error", error);
		return "customer/newCustomer";
	}
}
